package com.ad.pet.app.shopcar.confirm;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.account.adapter.OrderDetailGoodAdapter;
import com.ad.pet.app.account.order.fragment.PacketInfoContract;
import com.ad.pet.app.account.order.fragment.PacketInfoPresenter;
import com.ad.pet.app.account.share.ShareDialog;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.PayMethodAll;
import com.ad.pet.models.remote.account.order.PayUrl;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.mall.Counpous;
import com.ad.pet.models.result.ResultBean;
import com.ad.pet.ui.CompleteBackCashDialog;
import com.ad.pet.ui.OpenRedBagDialog;
import com.ad.pet.utils.UMEventUtil;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/3/5.
 */

public class CompleteOrderActivity extends AppToolbarActivity implements PacketInfoContract.View, ConfirmOrderContracts.View {


    @BindView(R.id.tv_order_number)
    TextView mTvOrderNumber;

    @BindView(R.id.tv_tips)
    TextView mTvTips;
    @BindView(R.id.recycler_order)
    RecyclerView mRecyclerOrder;

    @BindView(R.id.tv_total)
    TextView mTvTotal;
    @BindView(R.id.tv_total_price)
    TextView mTvTotalPrice;

    @BindView(R.id.tv_ship_way)
    TextView mTvShipWay;


    @Inject
    PacketInfoPresenter mPacketInfoPresenter;
    private OrderDetailGoodAdapter mOrderTypeAdapter;

    CompleteBackCashDialog mCompleteBackCashDialog;
    OpenRedBagDialog mOpenRedBagDialog;
    private TextView mTvDialogGet;
    private TextView mTvDialogValue;
    private TextView mTvDialogHasIn;

    private TextView mTvCongration;
    private TextView mTvRedValue;
    private TextView mTvCanCash;
    private TextView mTv_unopen;
    private Button mBtnHelp;

    @Inject
    ConfirmOrderPresenter mConfirmOrderPresenter;
    private SignCheck mSignCode;
    private ShareDialog mShareDialog;
    private UserBuyReturn mUserBuyReturn;
    //    private ShareBoardConfig mBoardConfig;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        setToolbarTitle(getString(R.string.paid_complete));
        mStatusLayoutManager.showContent();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_complete_order;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mPacketInfoPresenter.onCreate(this);
        mPacketInfoPresenter.payCompleted(getIntent().getStringExtra(Constant.ORDER_SN));


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerOrder.setLayoutManager(linearLayoutManager);
        mOrderTypeAdapter = new OrderDetailGoodAdapter(null);
        mRecyclerOrder.setAdapter(mOrderTypeAdapter);

        mCompleteBackCashDialog = new CompleteBackCashDialog(mActivity, R.style.custom_dialog_style);
        mTvDialogGet = mCompleteBackCashDialog.findViewById(R.id.tv_get);
        mTvDialogValue = mCompleteBackCashDialog.findViewById(R.id.tv_value);
        mTvDialogHasIn = mCompleteBackCashDialog.findViewById(R.id._tv_has_in);


        mOpenRedBagDialog = new OpenRedBagDialog(mActivity, R.style.custom_dialog_style);
        mTvCongration = mOpenRedBagDialog.getView().findViewById(R.id.tv_cash);
        mTvRedValue = mOpenRedBagDialog.getView().findViewById(R.id.tv_value);
        mTvCanCash = mOpenRedBagDialog.getView().findViewById(R.id.tv_cancash);
        mTv_unopen = mOpenRedBagDialog.getView().findViewById(R.id.tv_unopen);
        mBtnHelp = mOpenRedBagDialog.getView().findViewById(R.id.btn_help);


//        mBoardConfig = new ShareBoardConfig();
//        mBoardConfig.setIndicatorVisibility(false);
//        mBoardConfig.setTitleText(getString(R.string.share));
//        mBoardConfig.setCancelButtonText(getString(R.string.cancel));

        mShareDialog = new ShareDialog(mActivity);
    }

    @Override
    protected void initData() {

        mConfirmOrderPresenter.onCreate(this);
        mConfirmOrderPresenter.shareH5Url();
    }

    @Override
    protected void initEvents() {

        mCompleteBackCashDialog.findViewById(R.id.iv_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUserBuyReturn!=null)
                mPacketInfoPresenter.payCompletedOpen(mUserBuyReturn.getOrder_sn());
            }
        });

        mBtnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMEventUtil.onEvent(mActivity,"share_order_complete_event");
                if (mSignCode != null) {
                    mOpenRedBagDialog.dismiss();
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_share_img);

                    mShareDialog.share(mSignCode.getUrl(),getString(R.string.share_title),bitmap,getString(R.string.share_desc));
                    mShareDialog.show();
//                    UMWeb mUMWeb = new UMWeb(mSignCode.getUrl());
//                    UMImage mUmImage = new UMImage(mActivity, R.drawable.ic_share_img);
//                    mUMWeb.setThumb(mUmImage);
//                    mUMWeb.setTitle(getString(R.string.share_title));
//                    mUMWeb.setDescription(getString(R.string.share_desc) );
//                    new ShareAction(mActivity)
//                            .withMedia(mUMWeb)
//                            .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.FACEBOOK)
//                            .setCallback(new UMShareLinstener()).open(mBoardConfig);
                }
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent())

                .activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void onConfirmReciveSuccess(ResultBean resultBean) {

    }

    @Override
    public void onCommentSuccess(ResultBean resultBean) {

    }

    @Override
    public void createOrderSuccess(PayUrl payUrl) {

    }


    @Override
    public void getPayWaysSuccess(PayMethodAll payMethodAll) {

    }

    @Override
    public void addPayAdvice() {

    }

    @Override
    public void payCompletedSuccess(PayCompleted payCompleted) {
        mOrderTypeAdapter.setNewData(payCompleted.getProduct_list());

        mTvTotal.setText(payCompleted.getLang().getShop_cart().get(3));
        mTvTotalPrice.setText(payCompleted.getCurrency_symbol() + payCompleted.getTotal());

        mTvTips.setText(payCompleted.getLang().getDesc().get(2) + "\n" + payCompleted.getLang().getDesc().get(3) + "\n" + payCompleted.getLang().getDesc().get(4));
        mTvOrderNumber.setText(payCompleted.getLang().getDesc().get(0) + payCompleted.getOrder_sn());

        mTvDialogGet.setText(payCompleted.getPop_head());
        mTvDialogValue.setText("("  + payCompleted.getPop_price() + ")");
        mTvDialogHasIn.setText(payCompleted.getPop_foot());
        mTvShipWay.setText(payCompleted.getShipping_name());
        mConfirmOrderPresenter.get_user_buy_return();
//        mCompleteBackCashDialog.show();

    }

    @Override
    public void payCompletedOpenSuccess(SignCheck signCheck) {
        mCompleteBackCashDialog.dismiss();
        mTvCongration.setText(signCheck.getStr_head());
        mTvRedValue.setText( "US $"+signCheck.getPrice());
        mTv_unopen.setText(signCheck.getStr_foot());
        mOpenRedBagDialog.show();
    }



    @Override
    public void getCouponsSuccess(List<Counpous> coupons) {

    }

    @Override
    public void checkCouponsSuccess(Coupons coupons) {

    }

    @Override
    public void confirmOrderSuccess(PayCompleted shop) {

    }

    @Override
    public void getUserBanceSuccess(Banlance banlance) {

    }

    @Override
    public void getShipingWaysSuccess(BigShipingw shippingWay) {

    }

    @Override
    public void paymentBalanceSuccess(int toPay) {

    }

    @Override
    public void checkIsSignSuccess(SignCheck signCheck) {

    }

    @Override
    public void daySignSuccess(SignCheck signCheck) {

    }

    @Override
    public void sharH5UrlSuccess(SignCheck signCheck) {
            mSignCode = signCheck;

    }

    @Override
    public void get_user_buy_return_success(UserBuyReturn userBuyReturn) {

        if (!TextUtils.isEmpty(userBuyReturn.getOrder_sn())){
            mUserBuyReturn = userBuyReturn;
            mCompleteBackCashDialog.show();

        }
    }





}
